<?php
//用户
namespace app\supplier\model;

use think\facade\Db;
use think\Model;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class User extends Model{
    protected $pk = 'uid';
    // 模型初始化
    protected static function init()
    {
        parent::init();
        //TODO:初始化内容
    }
    
    //单个用户
    public function find($where){
        $find = $this::name('user')
                ->field('u.*')
                ->leftjoin('user_amount a','u.uid = a.uid')
                ->alias('u')
                ->where($where)
                ->find();
        return $find;
    }


    public function info($where){
        $find = $this::name('user')
            ->field('*')
            ->where($where)
            ->find();
        return $find;
    }
    
    //添加用户
    public function add_user($data){
        $in = $this::name('user')->insertGetId($data);
        return $in;
    }
    
    
    //登录验证
    public function find_info($username,$password){
        $info = $this::name('user')
        ->where('username','=',$username)
        ->where('password','=',$password)
        ->find();
        return $info;
    }

    
//    public function find_T_info($uid,$tpassword){
//        $info = $this::name('user')
//        ->where('uid','=',$uid)
//        ->where('t_password','=',$tpassword)
//        ->find();
//        return $info;
//    }
//    
    //修改密码
    public function changePassword($uid,$password){
        $in = $this::update(['password' => $password], ['uid' => $uid]);
        return $in;
    }
    
        //修改密码
    /*public function changeTPassword($uid,$t_password){
        $in = $this::update(['t_password' => $t_password], ['uid' => $uid]);
        return $in;
    }*/
    
    //最后登录时间
    public function lasttime($uid){
        $in = $this::update(['lasttime' => time()], ['uid' => $uid]);
        return $in;
    }
    
    public function userUpdate($uid,$update)
    {
        $in = $this::update($update, ['uid' => $uid]);
        return $in;
    }


    //店铺绑定+1
//    public static function userIpn($where){
//        return self::where($where)->inc('pay_shop_num', 1)->update();
//    }
//
//    //店铺绑定-1
//    public static function userDpn($where){
//        return self::where($where)->dec('pay_shop_num', 1)->update();
//    }
    
    //代理设置
//    public function proxySet($uid,$agent){
//        $in = $this::update(['agent' => $agent], ['uid' => $uid]);
//        return $in;
//    }
    
    //独立定价设置
//    public function pricing($uid,$in_pricing){
//        $in = $this::update(['in_pricing' => $in_pricing], ['uid' => $uid]);
//        return $in;
//    }
    
    public function listUser($page=1,$num=10,$where) {
        $list = $this::name('user')
        ->field('u.*,r.name as rname')
        ->alias('u')
        //->leftJoin('store s','u.stid = s.id')
        ->leftJoin('role_user ru','ru.uid = u.uid')
        ->leftJoin('role r','r.id = ru.rid')
        ->where($where)
        ->limit(($page-1)*$num,$num)
        ->select();
        return $list;
    }
    

    
    public function countUser($where) {
        return  $this::name('user')
        ->alias('u')
        //->leftJoin('store s','u.stid = s.id')
        ->leftJoin('role_user ru','ru.uid = u.uid')
        ->leftJoin('role r','r.id = ru.rid')
        ->where($where)
        ->count();
//        $count = $list->total();

    }


    
        //销售CRM  公海列表获取
    public static function merchantList($where,$page=1,$num=10,$order='a.uid',$joinA=' left join ',$joinF=' left join ',$whereA='',$whereF=''){
        
        /*$sql = 'select a.uid,a.phone,a.channel,a.channel_uid,a.admin_salesman_id,a.admin_salesman_link_up_status,a.member_pay_status,a.addtime,a.admin_link_up_time,a.admin_salesman_origin,a.admin_salesman_time,a.channel_keyword,a.lasttime,a.start_time,a.end_time,a.mem_name,if(b.tnum,b.tnum,0) as tnum,b.td
,a.etime,a.activity_count from (
select u.uid,u.phone,u.channel,u.channel_uid,u.admin_salesman_id,u.admin_salesman_link_up_status,u.member_pay_status,u.addtime,u.admin_link_up_time,u.admin_salesman_origin,u.admin_salesman_time as admin_salesman_time,u.channel_keyword,u.lasttime,any_value(um.start_time) as start_time,any_value(um.end_time) as end_time,any_value(ml.mem_name) as mem_name,
max(a.addtime) as etime,count(if(a.state>2,a.id,null)) as activity_count
from pg_user as u
left join pg_user_member as  um on um.uid = u.uid
left join pg_member_leve as ml on ml.id = um.m_id
left join pg_activity as a on a.uid = um.uid
where '.$where.' 
group by u.uid
order by '.$order.' desc
) as a
left join (
select u.uid,count(t.state) as tnum,max(t.addtime) as td
from pg_user as u
left join pg_user_member as  um on um.uid = u.uid
left join pg_member_leve as ml on ml.id = um.m_id
left join pg_flow_task as t on t.uid=um.uid
where '.$where.' and t.state>1
group by u.uid
) as b on a.uid=b.uid limit  '.($page-1)*$num.','.$num ;*/
      //  echo Db::name('user')->getLastSql();die;
        $sql ='select a.uid,a.phone,a.channel,a.channel_uid,a.admin_salesman_id,a.admin_salesman_link_up_status,a.member_pay_status,a.addtime,a.admin_link_up_time,a.admin_salesman_origin,a.admin_salesman_time,a.channel_keyword,a.lasttime,a.start_time,a.end_time,a.mem_name,if(b.tnum,b.tnum,0) as tnum,b.td
,c.etime,if(c.activity_count,c.activity_count,0) as activity_count from (
select u.uid,u.phone,u.channel,u.channel_uid,u.admin_salesman_id,u.admin_salesman_link_up_status,u.member_pay_status,u.addtime,u.admin_link_up_time,u.admin_salesman_origin,u.admin_salesman_time as admin_salesman_time,u.channel_keyword,u.lasttime,any_value(um.start_time) as start_time,any_value(um.end_time) as end_time,any_value(ml.mem_name) as mem_name
from sw_user as u
left join sw_user_member as  um on um.uid = u.uid
left join sw_member_leve as ml on ml.id = um.m_id
where '.$where.' 
group by u.uid 
) as a
'.$joinF.'(
select u.uid,count(t.state) as tnum,max(t.addtime) as td
from sw_user as u
left join sw_user_member as  um on um.uid = u.uid
left join sw_member_leve as ml on ml.id = um.m_id
left join sw_flow_task as t on t.uid=um.uid
where '.$where.' and t.state>1 '.$whereF.'
group by u.uid
) as b on a.uid=b.uid
'.$joinA.'(
select u.uid,max(a.addtime) as etime,count(if(a.state>2,a.id,null)) as activity_count
from pg_user as u
left join sw_user_member as  um on um.uid = u.uid
left join sw_member_leve as ml on ml.id = um.m_id
left join sw_activity as a on a.uid = um.uid
where '.$where.' and a.state>2 and a.type not in (5,6) '.$whereA.'
group by u.uid 
) as c on a.uid=c.uid 
order by '.$order.' desc limit  '.($page-1)*$num.','.$num ;
        return Db::query($sql);
    }

//    //销售CRM  公海列表获取
//    public static function merchantList($where,$page,$num){
//        $a= Db::name('user')
//            ->alias('u')
//            ->field('u.uid,u.phone,u.channel,u.channel_uid,u.admin_salesman_id,u.admin_salesman_link_up_status,u.member_pay_status,u.addtime,u.admin_link_up_time,u.admin_salesman_origin,any_value(um.start_time) as start_time,any_value(um.end_time) as end_time,any_value(ml.mem_name) as mem_name,max(a.addtime) as etime,count(a.addtime) as activity_count')
//            ->join('user_member um','um.uid = u.uid')
//            ->join('member_leve ml','ml.id = um.m_id')
//            ->leftjoin('activity a ','a.uid = um.uid')
//            ->where($where)
//            ->page($page,$num)
//            ->order('u.uid desc')
//            ->select();
//      //  echo Db::name('user')->getLastSql();die;
//        return $a;
//    }
        //销售CRM  公海总条数获取
    public static function merchantCount($where,$joinA=' left join ',$joinF=' left join ',$whereA='',$whereF=''){
        $sql ='select count(*) as count  from (
select u.uid,u.phone,u.channel,u.channel_uid,u.admin_salesman_id,u.admin_salesman_link_up_status,u.member_pay_status,u.addtime,u.admin_link_up_time,u.admin_salesman_origin,u.admin_salesman_time as admin_salesman_time,u.channel_keyword,u.lasttime,any_value(um.start_time) as start_time,any_value(um.end_time) as end_time,any_value(ml.mem_name) as mem_name
from sw_user as u
left join sw_user_member as  um on um.uid = u.uid
left join sw_member_leve as ml on ml.id = um.m_id
where '.$where.' 
group by u.uid 
) as a
'.$joinF.'(
select u.uid,count(t.state) as tnum,max(t.addtime) as td
from sw_user as u
left join sw_user_member as  um on um.uid = u.uid
left join sw_member_leve as ml on ml.id = um.m_id
left join sw_flow_task as t on t.uid=um.uid
where '.$where.' and t.state>1 '.$whereF.'
group by u.uid
) as b on a.uid=b.uid
'.$joinA.'(
select u.uid,max(a.addtime) as etime,count(if(a.state>2,a.id,null)) as activity_count
from pg_user as u
left join sw_user_member as  um on um.uid = u.uid
left join sw_member_leve as ml on ml.id = um.m_id
left join sw_activity as a on a.uid = um.uid
where '.$where.' and a.state>2 and a.type not in (5,6) '.$whereA.'
group by u.uid 
) as c on a.uid=c.uid ';
        return Db::query($sql);
    }

    //销售CRM  公海总条数获取
    public static function merchantCount_old($where){
        return Db::name('user')
            ->alias('u')
            ->join('user_member um','um.uid = u.uid')
            ->where($where)
            ->order('u.uid desc')
            ->count();
    }

    //销售CRM 全部商家  -- 列表
    public static function merchantAllList($where,$page,$num){
        dump($where);
        $pageIndex = ($page - 1) * $num;
        $sql = "
            SELECT
                u.uid,
                u.username,
                u.phone,
                u.admin_link_up_time,
                u.member_pay_status,
                u.addtime,
                um.m_id,
                um.start_time,
                um.end_time,
                ml.mem_name,
                IFNULL(u.adminname, '') AS adminname
            FROM
                (
                    SELECT
                        a.uid,
                        a.username,
                        a.phone,
                        a.admin_link_up_time,
                        a.member_pay_status,
                        a.addtime,
                        a.type,
                        b.username AS adminname
                    FROM
                        pg_user a
                    LEFT JOIN sw_user b ON b.uid = a.admin_salesman_id
                    GROUP BY
                        a.uid
                ) u
            LEFT JOIN sw_user_member um ON um.uid = u.uid
            LEFT JOIN sw_member_leve ml ON ml.id = um.m_id
            WHERE
                $where
            ORDER BY
                uid
            LIMIT $pageIndex,$num
        ";
        print_r($sql);
        return Db::query($sql);
    }



    //销售CRM  批量分配客户给销售
    public function merchantAllocate($where,$data){
        return Db::name('user')->where($where)->update($data);
    }

    //批量修改
    public function userUpdateAll($where,$data){
        return $this::update($data, $where);
    }


    //渠道商列表
    public static function useChannelList($where,$pageIndex,$pageSize){
        return Db::name('user')
            ->alias('u')
            ->field('u.uid,u.phone,uc2.spread,uc2.id as spread_id,uc1.channel_name,uc1.id as channel_id,u.enable,u.addtime')
            ->join('user_channel uc1','uc1.uid = u.uid')
            ->join('user_channel uc2','uc2.id = uc1.p_id')
            ->order('u.uid desc')
            ->where($where)
            ->page($pageIndex,$pageSize)
            ->select();
    }

    //获取渠道商总条数
    public static function userChannelCount($where){
        return Db::name('user')
            ->alias('u')
            ->join('user_channel uc1','uc1.uid = u.uid')
            ->join('user_channel uc2','uc2.id = uc1.p_id')
            ->where($where)
            ->count();
    }

    //渠道商邀请的商家列表
    public static function userChannelMerchantList($where,$pageIndex,$pageSize){
        return Db::name('user')
            ->alias('mu')
            ->field('mu.*')
            ->join('user cu','cu.uid = mu.channel_uid')
            ->where($where)
            ->order('mu.uid desc')
            ->page($pageIndex,$pageSize)
            ->select();
    }

    //渠道商邀请的商家统计
    public static function userChannelMerchantCount($where){
        return Db::name('user')
            ->alias('mu')
            ->field('mu.*')
            ->join('user cu','cu.uid = mu.channel_uid')
            ->where($where)
            ->count();
    }

    //管理员添加
    public  function userAdd(array $data){
        return self::create($data);
    }
    
        //管理员变更
    public function userEdit($uid,$update){
        $in = $this::update($update, ['uid' => $uid]);
        return $in;
    }

    public function userEditByWhere($where,$update){
        $in = $this::where($where)->update($update);
        return $in;
    }


    public  function userMemberJK($sql){

        return $this::query($sql);
        
    }
    
    public static function userSelect($where){
        return self::where($where)->select();
    }
    
}
